<template>
  <div class="login_content">
    <div class="login_box">
      <div class="title_box">
        <img src="../assets/logo.png" alt=""/>
      </div>
      <el-form class="login_form" :model="form_data" :rules="loginFrmRules" ref="loginfrmref">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form_data.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-s-cooperation"
            v-model="form_data.password"
            type="password"
          ></el-input>
        </el-form-item >
        <div class="login_btn">
          <el-button type="primary" @click="loginbtn">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data: function () {
    return {
      form_data: {
        username: 'admin',
        password: '123456'
      },
      loginFrmRules:{
        username:[
          { required: true, message: '请输入用户姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    reset: function () {
      this.$refs.loginfrmref.resetFields();
    },
    loginbtn:function () {
      this.$refs.loginfrmref.validate(valid=>{
        if(!valid) return;
        this.$axios.post("login",this.form_data).then(
          (data)=> {
            if (data.data.meta.status!==200) return this.$message.error("登录失败!");
            this.$message.success("登录成功！");
            //将token保存到session中，用于后期的身份认证
            var res_token=data.data.data.token;
            console.log(res_token)
            window.sessionStorage.setItem("token",res_token);
            this.$router.push("/home");
          }
        )
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login_content {
    background-color: #2b4b6b;
    color: white;
    width: 100%;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title_box {
      width: 160px;
      height: 160px;
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
      border: 1px solid #eee;
      border-radius: 50%;
      box-sizing: border-box;
      background-color: white;
      padding: 10px;
      box-shadow: 0 0 10px;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #eee;
        box-sizing: border-box;
        background-color: #eee;
      }
    }

    .login_form {
      width: 80%;
      height: 100%;
      margin: 100px auto;

      .login_btn {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
</style>
